package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.EmployeeService;
import org.apache.xmlbeans.impl.xb.ltgfmt.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        R<Employee> r = employeeService.selectEmployeeByUsernameAndPassword(employee);

        session.setAttribute("employee", r.getData().getId());

        return r;
    }

    @RequestMapping("/page")
    public R<Page<Employee>> page(long page, long pageSize, String name){
        Page<Employee> employeePage = employeeService.selectByPage(page, pageSize, name);

        R<Page<Employee>> success = R.success(employeePage);

        return success;
    }

    @RequestMapping("/logout")
    public R<String> logout(HttpSession session){
        session.invalidate();
        return R.success("退出登录成功");
    }

    @PostMapping()
    public R<String> add(@RequestBody Employee employee,HttpSession session){
        Long id = (Long) session.getAttribute("employee");

        employee.setCreateUser(id);
        employee.setUpdateUser(id);

        R<String> success = employeeService.add(employee);
        return success;
    }

    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable Integer id){
        Employee byId = employeeService.findById(id);

        return R.success(byId);
    }

    @PutMapping()
    public R<String> update(@RequestBody Employee employee){
        Boolean update = employeeService.update(employee);

        if (update){
            return R.success("...");
        }

        return R.error("员工信息修改失败!");
    }
}
